<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.2">
<title>Mapping types</title>
<link rel="stylesheet" href="./css/hibernate.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script>document.addEventListener('DOMContentLoaded', prettyPrint)</script>
</head>
<body class="article">
<div id="header">
</div>
<div id="content">
<div class="sect2">
<h3 id="mapping-types">Mapping types</h3>
<div class="paragraph">
<p>Hibernate understands both the Java and JDBC representations of application data.
The ability to read/write this data from/to the database is the function of a Hibernate <em>type</em>.
A type, in this usage, is an implementation of the <code>org.hibernate.type.Type</code> interface.
This Hibernate type also describes various aspects of behavior of the Java type such as how to check for equality, how to clone values, etc.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="title">Usage of the word <em>type</em></div>
<div class="paragraph">
<p>The Hibernate type is neither a Java type nor a SQL data type.
It provides information about both of these as well as understanding marshalling between.</p>
</div>
<div class="paragraph">
<p>When you encounter the term type in discussions of Hibernate, it may refer to the Java type, the JDBC type, or the Hibernate type, depending on context.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>To help understand the type categorizations, let&#8217;s look at a simple table and domain model that we wish to map.</p>
</div>
<div id="mapping-types-basic-example" class="exampleblock">
<div class="title">Example 1. Simple table and domain model</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-SQL" data-lang="SQL">create table Contact (
    id integer not null,
    first varchar(255),
    last varchar(255),
    middle varchar(255),
    notes varchar(255),
    starred boolean not null,
    website varchar(255),
    primary key (id)
)</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-JAVA" data-lang="JAVA">@Entity(name = "Contact")
public static class Contact {

    @Id
    private Integer id;

    private Name name;

    private String notes;

    private URL website;

    private boolean starred;

    //Getters and setters are omitted for brevity
}

@Embeddable
public class Name {

    private String first;

    private String middle;

    private String last;

    // getters and setters omitted
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In the broadest sense, Hibernate categorizes types into two groups:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#categorization-value">Value types</a></p>
</li>
<li>
<p><a href="#categorization-entity">Entity types</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="categorization-value">Value types</h4>
<div class="paragraph">
<p>A value type is a piece of data that does not define its own lifecycle.
It is, in effect, owned by an entity, which defines its lifecycle.</p>
</div>
<div class="paragraph">
<p>Looked at another way, all the state of an entity is made up entirely of value types.
These state fields or JavaBean properties are termed <em>persistent attributes</em>.
The persistent attributes of the <code>Contact</code> class are value types.</p>
</div>
<div class="paragraph">
<p>Value types are further classified into three sub-categories:</p>
</div>
<div class="dlist">
<dl>
<dt class="hdlist1">Basic types</dt>
<dd>
<p>in mapping the <code>Contact</code> table, all attributes except for name would be basic types. Basic types are discussed in detail in <a href="chapters/domain/basic_types.html#basic"><em>Basic Types</em></a></p>
</dd>
<dt class="hdlist1">Embeddable types</dt>
<dd>
<p>the name attribute is an example of an embeddable type, which is discussed in details in <a href="chapters/domain/embeddables.html#embeddables"><em>Embeddable Types</em></a></p>
</dd>
<dt class="hdlist1">Collection types</dt>
<dd>
<p>although not featured in the aforementioned example, collection types are also a distinct category among value types. Collection types are further discussed in <a href="chapters/domain/collections.html#collections"><em>Collections</em></a></p>
</dd>
</dl>
</div>
</div>
<div class="sect3">
<h4 id="categorization-entity">Entity types</h4>
<div class="paragraph">
<p>Entities, by nature of their unique identifier, exist independently of other objects whereas values do not.
Entities are domain model classes which correlate to rows in a database table, using a unique identifier.
Because of the requirement for a unique identifier, entities exist independently and define their own lifecycle.
The <code>Contact</code> class itself would be an example of an entity.</p>
</div>
<div class="paragraph">
<p>Mapping entities is discussed in detail in <a href="chapters/domain/entity.html#entity"><em>Entity</em></a>.</p>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2017-02-16 12:14:38 +01:00
</div>
</div>
</body>
</html>